//
//  OilCardVC.m
//  BasePorject_App
//
//  Created by Yutian Duan on 2019/5/27.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "OilCardVC.h"
#import "OilHeadView.h"
#import "OilAccountVC.h"
#import "OilMarketVC.h"
#import "OilLoginVC.h"
#import "ApplyOilFirstVC.h"

#import <LCActionSheet/LCActionSheet.h>

@interface OilCardVC () <LTSimpleScrollViewDelegate>

///!
@property (nonatomic, strong) OilHeadView *headView;

///! 控制器 和 title
@property (nonatomic, copy) NSArray <BaseViewController *> *viewControllers;
@property (nonatomic, copy) NSArray <NSString *> *titles;

@property (nonatomic, strong) LTLayout *layout;
@property (nonatomic, strong) LTSimpleManager *managerView;


@end

@implementation OilCardVC

- (OilHeadView *)headView {
  if (!_headView) {
    _headView = [[OilHeadView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth * ITEMPRO)];
    
    __weak typeof(self) weakSelf = self;
    _headView.bindOilClick = ^{
      [weakSelf showOilSheet];
    };
    
  }
  return _headView;
}

- (NSArray <NSString *> *)titles {
  if (!_titles) {
    _titles = @[YTLocalizedString(@"我的户口"), YTLocalizedString(@"市场咨询")];
  }
  return _titles;
}

- (NSArray <UIViewController *> *)viewControllers {
  if (!_viewControllers) {
    
    OilAccountVC *accountVC = [[OilAccountVC alloc] init];
    OilMarketVC *marketVC = [[OilMarketVC alloc] init];
    
    ///! 登录成功
    _viewControllers = @[accountVC, marketVC];
  }
  return _viewControllers;
}



- (LTSimpleManager *)managerView {
  if (!_managerView) {
    
    _layout = [[LTLayout alloc] init];
    _layout.bottomLineHeight = 2.0;
    _layout.bottomLineCornerRadius = 2.0;
    _layout.isAverage = YES;
    _layout.titleColor = DEF_GRAY;
    _layout.titleSelectColor = HEXCOLOR(0x871913);
    //
    _layout.bottomLineColor = HEXCOLOR(0x871913);
    _layout.sliderWidth = ScreenWidth/4;
    _layout.titleViewBgColor = HEXCOLOR(0xf7f8fc);
    _managerView = [[LTSimpleManager alloc] initWithFrame:CGRectMake(0, NavBarHeight, self.view.bounds.size.width, ScreenHeight - NavBarHeight - TabBarHeight) viewControllers:self.viewControllers titles:self.titles currentViewController:self layout:self.layout];
    
    
    /* 设置代理 监听滚动 */
    _managerView.delegate = self;
    /* 设置悬停位置 */
    //        _managerView.hoverY = 64;
    
  }
  return _managerView;
}


- (void)viewDidLoad {
  [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:animated];
  [self reloadHeadView];
  [self setStatusBarStyleWhite];
}

- (void)createFields {
  [super createFields];
  self.view.backgroundColor = [UIColor blackColor];
 
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(loginSucessed)
                                               name:NOTICE_LOGIN_APP
                                             object:nil];

}

- (void)createViews {
  [super createViews];
  [self.view addSubview:self.managerView];

}

- (void)createConfig {
  [super createConfig];
  
  
  __weak typeof(self) weakSelf = self;
  
  //配置headerView
  [self.managerView configHeaderView:^UIView * _Nullable{
    return weakSelf.headView;
  }];
  
  //pageView点击事件
  [self.managerView didSelectIndexHandle:^(NSInteger index) {
    NSLog(@"点击了 -> %ld", index);
    [weakSelf.viewControllers[index] loadData];
  }];
  
  //控制器刷新事件
  [self.managerView refreshTableViewHandle:^(UIScrollView * _Nonnull scrollView, NSInteger index) {
    NSLog(@"刷新");
    
    
    
  }];
  
}

- (void)reloadHeadView {
  ///! 导航栏
  if ([ToolsServer sharedInstance].isBindCard == YES) {
    [self setNavImage:[UIImage imageNamed:@"Home_BgImage"] titleColor:[UIColor whiteColor]];
  } else {
    [self setNavImage:[UIImage imageNamed:@"Oil_head_login"] titleColor:[UIColor whiteColor]];
  }
  ///! 段头
  [self.headView reloadView];
  
  ///! 修改控制器
  [self.viewControllers makeObjectsPerformSelector:@selector(loadData)];
  
}

///! 登录成功之后
- (void)loginSucessed {
  [self reloadHeadView];
  [self.viewControllers[self.managerView.currentSelectIndex] loadData];
}

///! 绑定成功
- (void)bindSucessed {
  
}

- (void)showOilSheet {
  
  __weak typeof(self) weakSelf = self;

  LCActionSheet *sheet = [[LCActionSheet alloc] initWithTitle:@"" cancelButtonTitle:@"取消" clicked:^(LCActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) {
    if (buttonIndex == 1) {
      [weakSelf showOilLogin];
    } else if (buttonIndex == 2) {
      [self applyNewOil];
    }
  } otherButtonTitles:YTLocalizedString(@"绑定现有油咭"),YTLocalizedString(@"申请新油咭"),nil];

  [sheet show];
  
  
}

- (void)showOilLogin {
  ///!
  OilLoginVC *loginVC = [[OilLoginVC alloc] init];
  loginVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
  
  [self presentViewController:loginVC
                     animated:YES
                   completion:^{
                     
                   }];

}

- (void)applyNewOil {
  
  ApplyOilFirstVC *applyVC = [[ApplyOilFirstVC alloc] init];
  applyVC.hidesBottomBarWhenPushed = YES;
  [self.navigationController pushViewController:applyVC animated:YES];
  
}

@end
